//
//  CZComposePictureViewCell.swift
//  CZVVeiBo
//
//  Created by smile on 2017/8/6.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

class CZComposePictureViewCell: UICollectionViewCell {
    
    // 声明一个闭包
    var closure:(()->())?

    // 提供属性
    var image: UIImage?{
        didSet{
            // 真实图片
            if let i = image {
                bgImageView.image = i
                bgImageView.highlightedImage = nil
                deleteButton.isHidden = false
            }else{
                // 加号图片
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteButton.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听事件
    @objc private func deletButtonClick(){
        closure?()
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        bgImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        deleteButton.snp.makeConstraints { (make) in
            make.top.right.equalTo(contentView)
        }
    }
    // MARK: - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView()
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deletButtonClick), for: UIControlEvents.touchUpInside)
        button.setImage(UIImage(named:"compose_photo_close"), for: UIControlState.normal)
        return button
    }()
}
